clear all

*Open the data*
use Clean_data, clear

************FIGURE 3***************
*Step 1: Raw gender gap*
bys year: egen avg_z_total_points_F= mean(z_total_points) if female==1 & testNOQUOTA==1 & foreign==0
bys year: egen avg_z_total_points_M= mean(z_total_points) if female==0 & testNOQUOTA==1 & foreign==0

egen avg_z_total_points_F2= max(avg_z_total_points_F), by (year)
egen avg_z_total_points_M2= max(avg_z_total_points_M), by (year)

gen avg_gender_gap = avg_z_total_points_F2-avg_z_total_points_M2



*Step 2: Gender gap controlling by GPA *
gen or = .
gen orlb = .
gen orub = .

* Step 3: Calculate marginal effects and store in the default frame

qui reg z_total_points i.female##i.year z_GPA  if degree=="M" & test==1 & (year<1987 | year>1988) & GPA!=. & foreign==0 , robust 

forvalues j=1983/2019{
if inlist(`j',1987,1988) continue
lincom _b[1.female] + _b[`j'.year#1.female] 
  
    replace or = r(estimate) if year == `j'
    replace orlb = r(lb) if year == `j'
    replace orub = r(ub) if year == `j'
}

* Step 3: Plot

twoway ///  
    (line inv_competNOQUOTA year if degree == "M" & year != 1987 & year != 1988 & female == 0, ///  
     lcolor(black) lpattern(solid)) ///  
    (line avg_gender_gap year if degree == "M" & year != 1987 & year != 1988 & female == 0, ///  
     lcolor(black) lpattern(longdash) ) ///  
    (rarea orlb orub year, ///  
     fcolor(gs6%50) fintensity(inten50) lwidth(none) legend(off)) ///  
    (line or year, ///  
     lcolor(black) lpattern(solid)), ///  
    graphregion(fcolor(white) lcolor(white)) ///  
    ylab(, axis(1) nogrid) ///  
    ytitle(" ", axis(1)) ///  
    xtitle(" ", size(small)) ///  
    xlabel(1983(6)2019, labsize(small) angle(45)) ///  
    yline(0, lcolor(grey) lpattern(shortdash)) ///	
legend(order(1 "Unconditional Probability of Getting a Position" 2 "Raw gender gap in total score" 3 "xxx" 4 "GPA-controlled gender gap in total score") ///
               cols(3) ///
               position(6) ///
               region(margin(0 0 0 0)) ///
               size(small)) 
